from django.db import models


class CodeTableHead(models.Model):
    LOCAL = (('zh-hans', '中文'), ('en', '英文'))
    localization = models.CharField(verbose_name='语言', max_length=10, choices=LOCAL)
    model = models.CharField(verbose_name='模块名', max_length=50, blank=True, null=True)
    field = models.CharField(verbose_name='字段名', max_length=50)

    class Meta:
        db_table = 'codetable_head'
        verbose_name = '代码对照头表'
        verbose_name_plural = verbose_name

    def __str__(self):
        model = self.model if self.model else ''
        return model + ' ' + self.field + ' ' + self.get_localization_display()


class CodeTableDetail(models.Model):
    codetable_head = models.ForeignKey(CodeTableHead, verbose_name='代码对照头表', on_delete=models.CASCADE)
    code = models.CharField(verbose_name='代码', max_length=10)
    name = models.CharField(verbose_name='名称', max_length=40)

    class Meta:
        db_table = 'codetable_detail'
        verbose_name = '代码对照明细表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.codetable_head.field + ' ' + self.code + ' ' + self.name
